package com.project.excel;

import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import static android.view.ViewGroup.LayoutParams.FILL_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static android.widget.LinearLayout.VERTICAL;


public class gridlayout extends Activity implements OnTouchListener {
	private CellView[][] cells;
	private int cellclicked;
	private int prevclicked;

	private int TOTAL_ROWS;
	private int TOTAL_COLS;

	private int prevclicked2;

	private int calSum;
	private EditText mainText;
	private Button go;
	private Button ref;
	private Button sum;
	private Button sort;
	private Button Rand;
	private Button Search;
	private Button product;
	private Button average;
	private Button subtract;
	private Button division;
	private CellView cell;
	private int referredCellId; 
	private String cellId;
	private int prevCellId;
	private boolean isReferred;
	private List<String> list;
	private ArrayAdapter<String> dataAdapter;
	private String TAG = "hello";
	private ArrayList <CellView> selectedData;
	private int test;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        LinearLayout linearLayout = new LinearLayout(this);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(FILL_PARENT, WRAP_CONTENT);
        linearLayout.setLayoutParams(params);
        linearLayout.setOrientation(VERTICAL);
        linearLayout.setBackgroundColor(Color.BLACK);
        list = new ArrayList<String>();
    	list.add("list 1");
    	list.add("list 2");
    	list.add("list 3");
    	dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
    	dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mainText = new EditText(this);
        mainText.setWidth(40);
        mainText.setHeight(105);
        TableLayout tableLayout2 = new TableLayout(this);
        tableLayout2.setStretchAllColumns(true);
        tableLayout2.setLayoutParams(params);
        ScrollView sclview2 = new ScrollView(this);
        HorizontalScrollView hrsclview2 = new HorizontalScrollView(this);
        TableRow tableRow2 = new TableRow(this);
        go=new Button(this);
        go.setText(">");
        go.setId(3000);
        go.setHeight(30);
        go.setWidth(40);
        go.setOnTouchListener(this);
        ref = new Button(this);
        ref.setText("Ref");
        ref.setId(3001);
        ref.setOnTouchListener(this);
        sum = new Button(this);
        sum.setText("SUM");
        sum.setId(3002);
        sum.setOnTouchListener(this);
        sort = new Button(this);
        sort.setText("Sort");
        sort.setId(3003);
        sort.setOnTouchListener(this);
        sort.setClickable(false);
        this.Rand = new Button(this);
        Rand.setText("Rand");
        Rand.setId(3004);
        Rand.setOnTouchListener(this);

        product = new Button(this);
        product.setText("Product");
        product.setId(3010);
        product.setOnTouchListener(this);
        
        average = new Button(this);
        average.setText("Average");
        average.setId(3009);
        average.setOnTouchListener(this);

        subtract = new Button(this);
        subtract.setText("Subtract");
        subtract.setId(3008);
        subtract.setOnTouchListener(this);
        
        division = new Button(this);
        division.setText("/");
        division.setId(3011);
        division.setOnTouchListener(this);

        this.Search = new Button(this);
        Search.setText("Search");
        Search.setId(3005);
        Search.setOnTouchListener(this);

        
        this.test = 0;
        this.prevclicked=0;
        this.prevclicked2=0;
        this.calSum=0;
        linearLayout.addView(mainText);
      /*  linearLayout.addView(go);
        linearLayout.addView(ref);
        linearLayout.addView(sum);
        linearLayout.addView(sort);
        linearLayout.addView(Rand);
<<<<<<< .mine
        linearLayout.addView(product);
        */
        
        tableRow2.addView(go);
        tableRow2.addView(ref);
        tableRow2.addView(sum);
        tableRow2.addView(sort);
        tableRow2.addView(Rand);
        tableRow2.addView(product);
        tableRow2.addView(average);
        tableRow2.addView(Search);
        tableRow2.addView(subtract);
        tableRow2.addView(division);
        tableLayout2.addView(tableRow2);
        sclview2.addView(hrsclview2);
        hrsclview2.addView(tableLayout2);
        linearLayout.addView(sclview2);
        //hrsclview2.addView(tableLayout2);
        //linearLayout.addView(hrsclview2);

     //   linearLayout.addView(Search);

        
        this.cellclicked = -1;
        this.isReferred = false;
        this.TOTAL_ROWS = 50;
        this.TOTAL_COLS = 28;
        this.selectedData = new ArrayList<CellView>();
        
        
        TableLayout tableLayout = new TableLayout(this);
        tableLayout.setStretchAllColumns(true);
        tableLayout.setLayoutParams(params);
        ScrollView sclview = new ScrollView(this);
        HorizontalScrollView hrsclview = new HorizontalScrollView(this);
        cells = new CellView[this.TOTAL_ROWS][this.TOTAL_COLS];
        for (int i=0; i<this.TOTAL_ROWS; i++)
        {
        	TableRow tableRow = new TableRow(this);
        	if (i==0)
        	{
        	cells[i][0]=  new CellView(this, i, 40, 40);
        	cells[i][0].setOnTouchListener(this);
    		tableRow.addView(cells[i][0]);
        	}
        	else
        	{
        		cells[i][0]=  new CellView(this, i, 40, 40);      		
        		cells[i][0].setCellText(i);
        		cells[i][0].setOnTouchListener(this);
        		tableRow.addView(cells[i][0]);
        	}
        	int k=65;
        	for (int j=1; j<28; j++)
        	{
        		if (i==0)
        		{
        			cells[i][j]=  new CellView(this, i, 40, 40);
            		cells[i][j].setCellId(28*i+j);
            		String aChar = new Character((char)k).toString();
            		cells[i][j].setCellText(aChar);
            		cells[i][j].setOnTouchListener(this);
            		tableRow.addView(cells[i][j]);
            		k=k+1;
        		}
        		else
        		{
        			cells[i][j]=  new CellView(this, i, 40, 40);
            		cells[i][j].setCellId(28*i+j);
            		cells[i][j].setOnTouchListener(this);
            		tableRow.addView(cells[i][j]);
        		}
        	}
        	tableLayout.addView(tableRow);
        }
       // tableLayout.setHorizontalScrollBarEnabled(true);
       
        sclview.addView(hrsclview);
        hrsclview.addView(tableLayout);
        linearLayout.addView(sclview);
       // linearLayout.addView(hrsclview);
        
        
        setContentView(linearLayout);
        
    }
    
    public boolean onTouch(View v, MotionEvent me)
    {   	
    	if (v instanceof CellView){
    		this.cell = (CellView) v;
    		this.cellclicked= this.cell.getCellId();
    		
    		if (this.isReferred){	//if the referred button is selected
    			
    			int i = this.referredCellId/28;
    	    	int j = this.referredCellId % 28;
    	    	String cellIdString = getCellId(i, j);
    	    	int parentI = i;
    	    	int parentJ = j;
    			//this.cells[i][j].getParentCell().add(cellIdString);
    			Object cellData = this.cells[i][j].getCellData();
    			
    			int currentCellId = this.cellclicked;
    			i = currentCellId/28;
    			j = currentCellId % 28;
    			this.cells[i][j].setCellData(cellData);
    			this.cells[i][j].setText(toString().valueOf(this.cells[i][j].getCellData()));
    			
    			this.cells[i][j].addParentCells(cells[parentI][parentJ]);	//add parent cell to the current cell
    			this.cells[parentI][parentJ].addChildren(cells[i][j]);	//the current to the parent
    			
    			this.updateData(cellData, i, j);
    			
    			if (cellData == null){
    				this.cells[i][j].setText(" ");
    			}
    			this.isReferred = false;
    		}
    		int temp = this.cellclicked;
        	int i = temp/28;
        	int j = temp % 28;
        	
        	/*
        	 * if 1 to 100 rows are selected
        	 */
        	
        	if ((i >= 1 && i <= this.TOTAL_ROWS) && j == 0 && (me.getAction() == MotionEvent.ACTION_DOWN)){
        		//left most cell is select
        		int numOfRows = this.TOTAL_ROWS;
        		int numOfCols = this.TOTAL_COLS;
        		
        		//if prev and current are same then deselect the row
        		if (this.cellclicked == this.prevclicked){
        			int prevRow = this.prevclicked/28;
        			int prevCol = this.prevclicked % 28;
        			for (int prevJ = 0; prevJ < this.TOTAL_COLS; ++prevJ){
        				this.cells[prevRow][prevJ].setBackgroundResource(R.drawable.back);
        			}
        			
        		}
        		
        		//if previous is clicked then deselect that row
        		else if (this.cellclicked != this.prevclicked){
        			int prevRow = this.prevclicked/28;
        			int prevCol = this.prevclicked % 28;
        			for (int prevJ = 0; prevJ < this.TOTAL_COLS; ++prevJ){
        				this.cells[prevRow][prevJ].setBackgroundResource(R.drawable.back);
        			}
        			this.selectedData = new ArrayList<CellView>();
            		//select all columns against that cell
            		for (int tempJ = 1; tempJ < numOfCols; ++tempJ){
            			if (this.cells[i][tempJ].getCellData() != null){
            				this.selectedData.add(this.cells[i][tempJ]);
            			}   			
            			this.cells[i][tempJ].setBackgroundResource(R.drawable.back2);
            		}
            		this.sort.setClickable(true);
        		}      		
        		
        	}
        	
        	///***************if A to Z cell is selected**********************///
        	if ((j >= 1 && j <= this.TOTAL_COLS) && i == 0 && (me.getAction() == MotionEvent.ACTION_DOWN)){
        		//left most cell is select
        		int numOfRows = this.TOTAL_ROWS;
        		int numOfCols = this.TOTAL_COLS;
        		
        		//if previous and current colums are same then deselect the column
        		if (this.cellclicked == this.prevclicked){
        			int prevRow = this.prevclicked/28;
        			int prevCol = this.prevclicked % 28;
        			for (int prevI = 0; prevI < this.TOTAL_ROWS; ++prevI){
        				this.cells[prevI][prevCol].setBackgroundResource(R.drawable.back);
        			}
        		}      		
        		
        		//if previous is clicked then deselect that row
        		else if (this.cellclicked != this.prevclicked){
        			int prevRow = this.prevclicked/28;
        			int prevCol = this.prevclicked % 28;
        			for (int prevI = 0; prevI < this.TOTAL_ROWS; ++prevI){
        				this.cells[prevI][prevCol].setBackgroundResource(R.drawable.back);
        			}
        			this.selectedData = new ArrayList<CellView>();
            		//select all ROWS against that cell
            		for (int tempI = 1; tempI < numOfRows; ++tempI){
            			if (this.cells[tempI][j].getCellData() != null){
            				this.selectedData.add(this.cells[tempI][j]);
            			}   			
            			this.cells[tempI][j].setBackgroundResource(R.drawable.back2);
            		}
            		this.sort.setClickable(true);
        		}   		
        	}
        	
        	//script to select and unselect a cell
        	if (me.getAction()==MotionEvent.ACTION_DOWN)
        	{
        		if (this.cellclicked!=this.prevclicked)
        		{
        			cells[i][j].setBackgroundResource(R.drawable.back2);
        			String temp3 =toString().valueOf(cells[i][j].getCellData());
        			if (temp3.equals("null")){
        				mainText.setText(" ");
        			}
            		else
            			mainText.setText(temp3);
        			
        			int temp2 = this.prevclicked;
                	int tempi = temp2/28;
                	int tempj = temp2 % 28;
                	cells[tempi][tempj].setBackgroundResource(R.drawable.back);
                	this.prevclicked2=this.prevclicked;
        			this.prevclicked=this.cellclicked;
        		}
        		else if (this.cellclicked==this.prevclicked)
        		{
        		cells[i][j].setBackgroundResource(R.drawable.back);
        		String temp3 =toString().valueOf(cells[i][j].getCellData());
        		if (temp3.equals("null")){
    				mainText.setText(" ");
    			}
        		else
        			mainText.setText(temp3);
        		this.prevclicked2=cellclicked;
        		this.prevclicked=0;
        		}
        		
        		if(this.cell.getCellId()==3000)
            	{
            		setText();
            		return true;
            	}      				
        	}          
    	}
    	else if (v instanceof Button){
    		
    		Button tempButton = (Button) v;
    		if (tempButton.getId() == 3001)	//if button clicked is reference
    		{
    			this.isReferred = true;
    			this.referredCellId = this.cellclicked;
    		}
    		
			else if(tempButton.getId() == 3002)	{
    			if (calSum==0)
    			{
    				calcSum();
    				return true;
    			}
    			else if (calSum==1)
    			{
    				calcSum();
        			return true;
    			}
			}
			else if(tempButton.getId() == 3010 && (me.getAction() == MotionEvent.ACTION_DOWN))
			{
				calProduct();
				return true;
			}
    		
			else if(tempButton.getId() == 3009 && (me.getAction() == MotionEvent.ACTION_DOWN))
			{
				calAverage();
				return true;
			}
			else if(tempButton.getId() == 3008 && (me.getAction() == MotionEvent.ACTION_DOWN))
			{
				calSubtract();
				return true;
			}
			else if(tempButton.getId() == 3011 && (me.getAction() == MotionEvent.ACTION_DOWN))
			{
				calDivision();
				return true;
			}
			else if(tempButton.getId() == 3003 && (me.getAction() == MotionEvent.ACTION_DOWN))	//if sort button is pressed
			{
				test++;
				this.selectedData = sort(this.selectedData);
				//set sort button to false clickable
				this.sort.setClickable(false);
				int i = this.cellclicked/28;
				int j = this.cellclicked % 28;
				Log.v(TAG, toString().valueOf(test));
				//sort row 
				if (j == 0){
					for (int index = 1, temp = 0; temp < this.selectedData.size(); ++index,++temp){
						this.cells[i][index].setText(toString().valueOf(this.selectedData.get(temp).getCellData()));
					}
				}
				if (i == 0){
					for (int index = 1, temp = 0; temp < this.selectedData.size(); ++index,++temp){
						this.cells[index][j].setText(toString().valueOf(this.selectedData.get(temp).getCellData()));
					}
				}
				
			}
			else if(tempButton.getId() == 3004 && (me.getAction() == MotionEvent.ACTION_DOWN)){	//generate random numbers
				Random randNum = new Random();
				for (int row = 1; row < this.TOTAL_ROWS; ++row){
					for (int col = 1; col < this.TOTAL_COLS; ++col){
						int num = randNum.nextInt(1000) + 1;
						this.cells[row][col].setCellData(num);
						this.cells[row][col].setText(toString().valueOf(this.cells[row][col].getCellData()));
					}
				}
			}
    		
			else if(tempButton.getId() == 3000)
        	{
    			setText();
        	}
    		
			else if (tempButton.getId() == 3005 && (me.getAction() == MotionEvent.ACTION_DOWN))	//search button is clicked
			{
				String searchText = toString().valueOf(mainText.getText());
				boolean check = true;
				int i = 0;
				int j = 0;
				for (; i < this.TOTAL_ROWS && check ; ++i){
					for (j = 0; j < this.TOTAL_COLS; ++j){
						Object data = this.cells[i][j].getCellData();
						if (data != null){
							int type = this.cells[i][j].getType(data);
							if (type == 0){
								if (searchText.equals(toString().valueOf(data)));
								{
									this.cells[i][j].setBackgroundResource(R.drawable.back2);
								}
							}
						}
						
					}
				}			
			}
    	}
    	
    	return true;  	
    }
    
    public void setText()
    {

    	if (this.cellclicked != -1){
	    	int temp = this.cellclicked;
	    	int i = temp/28;
	    	int j = temp % 28;
	    	this.cellId = getCellId(i , j);
	    	CharSequence c = mainText.getText();
	    	String s = c.toString();
	    	cells[i][j].setCellData(s);
	    	cells[i][j].setText(c);
	    	//Log.v(TAG, (String)cells[i][j].getCellData());
	    	int obj = cells[i][j].getType(c);
	    	Object data = cells[i][j].getCellData();
	    	updateData(data, i, j);
	    	//Log.v("data", toString().valueOf(data));
	    	this.cellclicked = -1;
    	}

    	

    	
    }
    
    public void getSum()
    {
    	
    	mainText.setText("=SUM(  :  )");
    	sum.setText("Cal");
    	this.calSum=1;
    	
    }
    
    public void calcSum()
    {
    	//String c = mainText.getText().toString();
    	int id1=this.prevclicked2;
    	int id2=this.cellclicked;
    	
    	int i = id1/28;
    	int j = id1 % 28;
    	
    	int i2 = id2/28;
    	int j2 = id2 % 28;
    	
    	int sum=0;
    	if (j==j2)
    	{
    		for (int k=i; k<=i2; k++)
    		{
    			String temp = toString().valueOf(cells[k][j].getCellData());
    			int temp2 =Integer.parseInt(temp);
    			sum=sum+temp2;
    		}
    		String textrep = Integer.toString(sum);
    		Log.v(TAG, textrep);
    		mainText.setText(textrep);
    	}	
    	else if (i==i2)
    	{
    		sum=0;
    		for (int k=j; k<=j2; k++)
    		{
    			
    			String temp =toString().valueOf(cells[i][k].getCellData());
    			int temp2 =Integer.parseInt(temp);
    			sum=sum+temp2;
    		}
    		String textrep = Integer.toString(sum);
    		Log.v(TAG, textrep);
    		mainText.setText(textrep);
    	}
    	
    	
    	
    	 
        //LinearLayout linearLayout2 = new LinearLayout(this);
        //ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(FILL_PARENT, WRAP_CONTENT);
    /*    linearLayout2.setLayoutParams(params);
        linearLayout2.setOrientation(VERTICAL);
        linearLayout2.setBackgroundColor(Color.BLACK);
    	*/
    //	Spinner spin = new Spinner(this);
    	//spin.setId(4000);
    //	spin.setLayoutParams(params);
    //	spin.setAdapter(dataAdapter);
    //	spin.performClick();
    	//linearLayout2.addView(spin);
    	
    	
    }
    
    public void calProduct()
    {
    	int id1=this.prevclicked2;
    	int id2=this.cellclicked;
    	
    	int i = id1/28;
    	int j = id1 % 28;
    	
    	int i2 = id2/28;
    	int j2 = id2 % 28;
    	
    	double product=0;
    	if (j==j2)
    	{
    		String temp =toString().valueOf(cells[i][j].getCellData());
    		product = Double.parseDouble(temp);
    		for (int k=i+1; k<=i2; k++)
    		{
    			temp =toString().valueOf(cells[k][j].getCellData());
    			double temp2 =Double.parseDouble(temp);
    			product=product*temp2;
    		}
    		String textrep = Double.toString(product);
    		Log.v(TAG, textrep);
    		mainText.setText(textrep);
    	}	
    	else if (i==i2)
    	{
    		String temp = toString().valueOf(cells[i][j].getCellData());
    		product = Double.parseDouble(temp);
    		for (int k=j+1; k<=j2; k++)
    		{
    			temp =toString().valueOf(cells[i][k].getCellData());
    			double temp2 =Double.parseDouble(temp);
    			product=product*temp2;
    		}
    		String textrep = Double.toString(product);
    		Log.v(TAG, textrep);
    		mainText.setText(textrep);
    	}
    	
    }
    
    public void calSubtract()
    {
    	//String c = mainText.getText().toString();
    	int id1=this.prevclicked2;
    	int id2=this.cellclicked;
    	
    	int i = id1/28;
    	int j = id1 % 28;
    	
    	int i2 = id2/28;
    	int j2 = id2 % 28;
    	
    	double subtract=0;
    	if (j==j2)
    	{
    		String temp =toString().valueOf(cells[i][j].getCellData());
    		subtract = Double.parseDouble(temp);
    		for (int k=i+1; k<=i2; k++)
    		{
    			temp =toString().valueOf(cells[k][j].getCellData());
    			double temp2 =Double.parseDouble(temp);
    			subtract=subtract-temp2;
    		}
    		String textrep = Double.toString(subtract);
    		Log.v(TAG, textrep);
    		mainText.setText(textrep);
    	}	
    	else if (i==i2)
    	{
    		String temp = toString().valueOf(cells[i][j].getCellData());
    		subtract = Double.parseDouble(temp);
    		for (int k=j+1; k<=j2; k++)
    		{
    			temp =toString().valueOf(cells[i][k].getCellData());
    			double temp2 =Double.parseDouble(temp);
    			subtract=subtract-temp2;
    		}
    		String textrep = Double.toString(subtract);
    		Log.v(TAG, textrep);
    		mainText.setText(textrep);
    	}
    	
    	
    	
    }
    
    public void calDivision()
    {
    	//String c = mainText.getText().toString();
    	int id1=this.prevclicked2;
    	int id2=this.cellclicked;
    	
    	int i = id1/28;
    	int j = id1 % 28;
    	
    	int i2 = id2/28;
    	int j2 = id2 % 28;
    	
    	double division=0;
    	double first=0;
    	double second=0;
    	String temp =toString().valueOf(cells[i][j].getCellData());
		first = Double.parseDouble(temp);
		temp =toString().valueOf(cells[i2][j2].getCellData());
		second = Double.parseDouble(temp);
		division=first/second;
		String textrep = Double.toString(division);
		Log.v(TAG, textrep);
		mainText.setText(textrep);
    }
    
    public void calAverage()
    {
    	int id1=this.prevclicked2;
    	int id2=this.cellclicked;
    	
    	int i = id1/28;
    	int j = id1 % 28;
    	
    	int i2 = id2/28;
    	int j2 = id2 % 28;
    	
    	double average=0;
    	if (j==j2)
    	{
    		String temp =toString().valueOf(cells[i][j].getCellData());
    		average = Double.parseDouble(temp);
    		for (int k=i+1; k<=i2; k++)
    		{
    			temp =toString().valueOf(cells[k][j].getCellData());
    			double temp2 =Double.parseDouble(temp);
    			average=average+temp2;
    		}
    		average=average/(i2-i+1);
    		String textrep = Double.toString(average);
    		Log.v(TAG, textrep);
    		mainText.setText(textrep);
    	}	
    	else if (i==i2)
    	{
    		String temp =toString().valueOf(cells[i][j].getCellData());
    		average = Double.parseDouble(temp);
    		for (int k=j+1; k<=j2; k++)
    		{
    			temp =toString().valueOf(cells[i][k].getCellData());
    			double temp2 =Double.parseDouble(temp);
    			average=average+temp2;
    		}
    		average=average/(j2-j+1);
    		String textrep = Double.toString(average);
    		Log.v(TAG, textrep);
    		mainText.setText(textrep);
    	}
    	
    }
    
    public String getCellId(int row, int col){
    	int alphaAscii = 65;	//set the ascii of initial alpha to 'A'
    	//Log.v(this.TAG, String.valueOf(row));
    	//Log.v(this.TAG, String.valueOf(col));
    	
    	col--;
    	int colAscii = alphaAscii + col;
    	String cellId = String.valueOf( (char)colAscii) + row;
    	Log.v(TAG, cellId);
    	
    	return cellId;
    	
    }
    
    public int getCellNumeric(String cellId){
    	char col = cellId.charAt(0);
    	String temp = toString().valueOf(col);
    	cellId = cellId.replace(temp,"");
    	cellId.trim();
    	int intId = Integer.parseInt(cellId);
    	int intAscii = (int) col;	//e.g. A = 65
    	int result = intAscii - 65 + 1;
    	result = intId * 28 + result;
    	//Toast.makeText(getBaseContext(),""+result, Toast.LENGTH_LONG).show();
    	
    	return result;
    	
    }
    
    public void updateData(Object data, int i, int j ){
    	
    	ArrayList<CellView> cellArray = this.cells[i][j].getChildren();
    	
		for (int k = 0; k < cellArray.size(); ++k){
			cellArray.get(k).setCellData(data);
			int id = cellArray.get(k).getCellId();
			int childI = id/28;
			int childJ = id % 28;
			this.cells[childI][childJ].setText(toString().valueOf(data));
			
			if (data == null){
				this.cells[childI][childJ].setText(" ");
			}
		}
    }
    
    public ArrayList <CellView> sort(ArrayList<CellView>dataToSort){
    	
    	for (int i = 1; i < dataToSort.size(); i++){
    		int j = i;
    		int B = (Integer) dataToSort.get(i).getCellData();
    		while ((j > 0) && ((Integer)dataToSort.get(j-1).getCellData() > B)){
    		  dataToSort.get(j).setCellData(dataToSort.get(j-1).getCellData());
    		  j--;
    		}
    		dataToSort.get(j).setCellData(B);
    	}
    	
    	return dataToSort;
    }
}
